import com.rabbitmq.client.*;



public class rpcClient {

	private static Connection connection;
	private static Channel channel;
	private static String requestQueueName = "rpc_queue";
	private static String replyQueueName;
	private static QueueingConsumer consumer;
	
	
	//private static  String rabbitmqServer = "localhost";
	private static String rabbitmqServer = "us01s-netops01";
	//private static final String rabbitmqServer = "rp-1";
	
	public static void main(String[] argv) throws Exception {
	
		String server = argv[0];
		
	    ConnectionFactory factory = new ConnectionFactory();
	    factory.setHost(rabbitmqServer);
	    factory.setVirtualHost("backup");
	    factory.setPassword("networker");
	    factory.setUsername("backup");
	    connection = factory.newConnection();

	    channel = connection.createChannel();


	    replyQueueName = channel.queueDeclare().getQueue(); 
	    consumer = new QueueingConsumer(channel);
	    channel.basicConsume(replyQueueName, true, consumer);
	    System.out.println(call(server));
	    close();
	}

	@SuppressWarnings("deprecation")
	public static String call(String message) throws Exception {     
	    String response = null;
	    String corrId = java.util.UUID.randomUUID().toString();
	    
	    AMQP.BasicProperties props = new AMQP.BasicProperties();
	    props.setCorrelationId(corrId);
	    props.setReplyTo(replyQueueName);
	    //props.setExpiration("100000");
	    
       // message = "Cool";

	    channel.basicPublish("", requestQueueName, props, message.getBytes());
	    System.out.println(message);
	    while (true) {
	        QueueingConsumer.Delivery delivery = consumer.nextDelivery();
	        
	        if (delivery.getProperties().getCorrelationId().equals(corrId)) {
	            response = new String(delivery.getBody());
	            break;
	        }
	    }

	    return response; 
	}

	public static void close() throws Exception {
	    connection.close();
	}
}

